<?php
include 'review_lib.php';
$b_Menu_Closed = true;

$a_css_files = array (
    array ( 'path' => 'plugins/bootstrap-modal/css/bootstrap-modal.css' ),
    array ( 'path' => 'css/pages/nh-review.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/jquery-file-upload/js/vendor/tmpl.min.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modal.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modalmanager.js' ),
    array ( 'path' => 'scripts/nh-gridviewScroll.min.js' ),
    array ( 'path' => 'scripts/nh-review.js' ),
);

$a_js_class[] = 'User_Review.init();';

$a_Total_ChuyenCan = $a_Users = $a_Tmp_User = $a_Tmp_Dot_KiemTra = $a_Tmp_ChuyenCan = $a_User_HocLuc = $a_Sundays = $a_User_ChuyenCan = $a_Tmp_SoLan_KiemTra = array ();
$s_Where = '';
//$a_Tmp_SoLan_KiemTra
// <editor-fold defaultstate="collapsed" desc="Make Where">
if ($s_category_team) {
    $db_query = "
        select  userid, chuyencan, hocluc, rank, classid, note
        from    user_class
        where   team = '$s_category_team' and level = '$s_category_level' and crew = '$s_category_crew' and course = '$s_category_course' ";

    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)) {
        $a_Users[$d_row->userid] = $d_row;
    }
    
    $s_Where = $s_Where  . ' and userid in ("' . implode('", "', array_keys($a_Users)) . '")';
}

make_where($s_Where, $a_filterings);
// </editor-fold>

if (!empty($a_Users)) {

    // <editor-fold defaultstate="collapsed" desc="Get Data">
    $db_query  = db_get_query($a_tablecolumns_namhoa_user) . $s_Where . " and $s_get_THIEU_NHI_condition " . ' order by firstname';
    $db_result = db_query($db_conn, $db_query);
    
    while ($d_row = db_fetch($db_result)) {
        $a_Tmp_User[$d_row->userid] = $d_row;
    }

    if (!empty($a_Tmp_User)) {

        $v_tmp_startdate = date( $s_category_course + 0 . '-m-d', strtotime($a_KhoaHoc_Info['start']));
        $v_tmp_enddate   = date( $s_category_course + 1 . '-m-d', strtotime($a_KhoaHoc_Info['end'  ]));

        $db_query = "
            select  userid, type, scoretype, date, score, data
            from    user_score
            where   userid in ('" . implode("', '", array_keys($a_Tmp_User)) . "') and date >= '$v_tmp_startdate' and date <= '$v_tmp_enddate'
            order by date";

        $db_result = db_query($db_conn, $db_query);

        while ($d_row = db_fetch($db_result)) {
            
            if ( $d_row->type == 'CHUYEN_CAN') {
                $a_Tmp_ChuyenCan    [$d_row->score ]                                     = isset($a_DiLe[$d_row->score]) ? $a_DiLe[$d_row->score] : @$a_DiHoc[$d_row->score] ;
                $a_Sundays          [$d_row->date  ]                                     = $d_row->date;
                $a_User_ChuyenCan   [$d_row->userid][$d_row->date ][$d_row->scoretype]   = $d_row->score;
                @$a_Total_ChuyenCan [$d_row->userid][$d_row->score]['counter']          += 1;
                @$a_Total_ChuyenCan [$d_row->userid][$d_row->score]['minus'  ]          += $d_row->data;
            }
            else if ( $d_row->type == 'HOC_LUC') {
                $a_Tmp_SoLan_KiemTra[$d_row->data]                                  = $d_row->data;
                $a_User_HocLuc[$d_row->userid   ][$d_row->data][$d_row->scoretype]  = $d_row->score;
                $a_Tmp_Dot_KiemTra [$d_row->scoretype]                             = $d_row->scoretype;
            }
        }
    }
    // </editor-fold>
}

$a_permission = get_passport_action_from_module($db_conn, array('TONG_KET'));

include 'lib_head.php';
?>
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div id="portlet-config" class="modal hide">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"></button>
        <h3>Bộ Lọc</h3>
    </div>
    <div class="modal-body">
        Điền thông tin cần thiết để lọc được dữ liệu thích hợp.
    </div>
</div>
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div class="row-fluid">
    <!-- BEGIN BORDERED TABLE PORTLET-->
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="icon-filter"></i>Lọc Dữ Liệu</div>
            <div class="tools">
                <a href="javascript:;" class="collapse"></a>
                <a href="#portlet-config" data-toggle="modal" class="config"></a>
            </div>
        </div>
        <div class="portlet-body">
            <form action="" style="margin: 0">
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Ngành</label>
                        <div class="controls">
                            <select name="category_team" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Nganh as $v_key => $v_value) { ?>
                                    <option <?= $s_category_team == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Cấp</label>
                        <div class="controls">
                            <select name="category_level" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Cap as $v_key => $v_value) { ?>
                                    <option <?= $s_category_level == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Đội</label>
                        <div class="controls">
                            <select name="category_crew" class="m-wrap span12">
                                <option value=""></option>
                                <?php foreach ($a_Doi as $v_key => $v_value) { ?>
                                    <option <?= $s_category_crew == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label">Khóa Học</label>
                        <div class="controls">
                            <select name="category_course" class="m-wrap span12">
                                <?php $v_tmp_start_course = $a_KhoaHoc_Info['start_year'];
                            for ($v_index = $v_tmp_start_course; $v_index > $v_tmp_start_course - 10 ; $v_index--) { ?>
                                <option <?= $s_category_course == $v_index ? 'selected="selected"' : '' ?> value="<?= $v_index ?>"><?= $v_index ?> - <?= $v_index + 1?></option>
                            <?php } ?>
                            </select>                            
                        </div>
                    </div>
                </div>
                <div class="pull-left">
                    <div class="control-group">
                        <label class="control-label">&ensp;</label>
                        <div class="controls">
                            <div class="input-prepend">
                                <button class="btn blue">BẮT ĐẦU LỌC <i class="m-icon-swapright m-icon-white"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </form>
        </div>
    </div>
    <!-- END BORDERED TABLE PORTLET-->
    <?php if (!empty($a_Tmp_User)) { ?>
    <div class="portlet-table" style="height: 520px;">
        <table class="table table-bordered table-hover data-user">
            <tr class="GridviewScrollHeader">
                <th rowspan="2">Họ và Tên</th>
                <?php foreach ($a_Sundays as $v_sundate) { ?>
                <th colspan="2"><?= namhoa_date($v_sundate) ?></th>
                <?php } ?>
                <th rowspan="2"></th>
                <?php foreach ($a_Tmp_Dot_KiemTra as $v_scoretype_value) { ?>
                <th colspan="<?= count($a_Tmp_SoLan_KiemTra ) + 1?>"><?= @$a_Dot_KiemTra[$v_scoretype_value]?></th>
                <?php } ?>
                <th rowspan="2"></th>
                <th colspan="<?= count($a_Tmp_ChuyenCan) + 2?>">Chuyên Cần</th>
                <th colspan="2">Học Tập</th>
                <th rowspan="2"></th>
                <th rowspan="2">Xét Duyệt</th>
            </tr>
            <tr class="GridviewScrollHeader">
                <?php foreach ($a_Sundays as $v_sundate) { ?>
                <th>Đi Lễ</th>
                <th>Học GL</th>
                <?php } ?>
                <?php
                foreach ($a_Tmp_Dot_KiemTra as $v_scoretype_value) {
                    foreach ($a_Tmp_SoLan_KiemTra as $v_key => $v_value) {?>
                <th><?=@$a_SoLan_KiemTra[$v_value]?></th>
                <?php } ?>
                <th>TB</th>
                <?php } ?>
                <?php foreach ($a_Tmp_ChuyenCan as $v_value) { ?>
                <th><?=$v_value?></th>
                <?php } ?>
                <th>TB</th>
                <th>Xếp Loại</th>
                <th>TB</th>
                <th>Xếp Loại</th>
            </tr>
        <?php foreach ($a_Tmp_User as $v_counter => $d_user) { ?>
            <tr class="GridviewScrollItem">
                <td style="text-align: left;"><?=$d_user->saint?> <?=$d_user->username?></td>
                <?php
                // <editor-fold defaultstate="collapsed" desc="Chuyen Can">
                foreach ($a_Sundays as $v_sundate) {

                    $v_tmp_le  = @$a_User_ChuyenCan[$d_user->userid][$v_sundate]['LE' ];
                    $v_tmp_hoc = @$a_User_ChuyenCan[$d_user->userid][$v_sundate]['HOC'];
                ?>
                <td>
                    <div><?=@$a_DiLe[$v_tmp_le]?></div>
                </td>
                <td>
                    <div><?=@$a_DiHoc[$v_tmp_hoc]?></div>
                </td>
                <?php
                }
                // </editor-fold>
                ?>
                <td></td>
                <?php
                // <editor-fold defaultstate="collapsed" desc="Hoc Luc">
                foreach ($a_Tmp_Dot_KiemTra as $v_scoretype_key => $v_scoretype_value) {
                    $v_total = array();
                    foreach ($a_Tmp_SoLan_KiemTra as $v_key => $v_value) {

                        $v_tmp_value = @$a_User_HocLuc[$d_user->userid][$v_key][$v_scoretype_key];
                        if ($v_tmp_value) { $v_total[] = $v_tmp_value; }
                ?>
                <td>
                    <div><?=$v_tmp_value?></div>
                </td>
                <?php } ?>
                <td>
                    <?= !empty($v_total) ? array_sum($v_total) / count($v_total) : '' ?>
                </td>
                <?php
                }
                // </editor-fold>
                ?>
                <td></td>
                <?php
                
                // <editor-fold defaultstate="collapsed" desc="Chua Co Du Lieu | Du Lieu Mac Dinh">
                $a_tmp_review = $a_Users[$d_user->userid];
                
                if (!$a_tmp_review->rank) {
                    $a_tmp_review->hocluc    = 10;
                    $a_tmp_review->chuyencan = 10;
                }
                // </editor-fold>

                $o_rank_chuyencan = get_rank_type($a_tmp_review->chuyencan, 'CHUYEN_CAN');
                $o_rank_hocluc    = get_rank_type($a_tmp_review->hocluc  , 'HOC_LUC');

                foreach ($a_Tmp_ChuyenCan as $v_key => $v_value) {
                    $v_tmp_counter = @$a_Total_ChuyenCan[$d_user->userid][$v_key]['counter'];
                    $v_tmp_minus   = @$a_Total_ChuyenCan[$d_user->userid][$v_key]['minus'];
                    $v_tmp_txt     = $v_tmp_counter ? "$v_tmp_counter | $v_tmp_minus" : '';
                ?>
                <td class="nowrap"><?=$v_tmp_txt?></td>
                <?php } ?>
                <td><?= $a_tmp_review->chuyencan?></td>
                <td><?= $o_rank_chuyencan->value?></td>
                <td><?= $a_tmp_review->hocluc ?></td>
                <td><?= $o_rank_hocluc->value ?></td>
                <td></td>
                <td class="text-center <?=!empty($a_permission) ? 'set-rank' : '' ?>" rank="<?=$a_tmp_review->rank?>" username="<?=$d_user->username?>" classid="<?=$a_tmp_review->classid?>" note="<?=$a_tmp_review->note?>" ><?=@$a_XepHang[$a_tmp_review->rank]?></td>
            </tr>
        <?php } ?>
        </table>
    </div>
    <?php } ?>
</div>
<div id="responsive" class="modal hide fade" tabindex="-1">
</div>
<script id="rank-tmpl" type="text/x-tmpl">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h3>{%=o.username%}</h3>
    </div>
    <div class="modal-body">
        <input type="hidden" name="classid" value="{%=o.classid%}"> 
        <div class="row-fluid">
            <div class="span12 ">
                <div class="control-group">
                    <label class="control-label">Xếp Loại</label>
                    <div class="controls">
                        <select name="rank" class="m-wrap span4">
                            <?php foreach ($a_XepHang as $v_key => $v_value) { ?>
                                <option value="<?=$v_key?>" {% if(o.rank == '<?=$v_key?>') { %} selected="selected" {% } %} ><?= $v_value ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span12 ">
                <div class="control-group">
                    <label class="control-label">Ghi Chú</label>
                    <div class="controls">
                        <textarea class="span12 m-wrap" name="note">{%=o.note%}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn">Thoát</button>
        <button type="button" class="btn blue btn-save">Lưu</button>
    </div>
</script>
<?php include 'lib_tail.php'; ?>